{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "64e5fbf6d4af18cf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义符号变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义函数 f(x)\n",
    "f = x**2 + 3*x + 2\n",
    "\n",
    "# 计算导数\n",
    "f_prime = sp.diff(f, x)\n",
    "print(\"导数 f'(x) =\", f_prime)\n"
   ],
   "id": "d73d1b5cae6c42b8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 导入必要的库\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "plt.rcParams.update({\n",
    "    'font.size': 14,          # 全局字体大小\n",
    "    'axes.titlesize': 16,     # 标题字体大小\n",
    "    'axes.labelsize': 14,     # 坐标轴标签字体大小\n",
    "    'xtick.labelsize': 12,    # x轴刻度字体\n",
    "    'ytick.labelsize': 12,    # y轴刻度字体\n",
    "    'legend.fontsize': 20     # 图例字体\n",
    "})\n",
    "# 初始化设置\n",
    "plt.rcParams['font.sans-serif'] = ['KaiTi']  # 中文字体\n",
    "plt.rcParams['mathtext.fontset'] = 'stix'     # 数学字体\n",
    "\n",
    "# 定义函数\n",
    "def f(x):\n",
    "    return x**2 + 3*x + 2\n",
    "\n",
    "def f_prime(x):\n",
    "    return 2*x + 3\n",
    "\n",
    "# 创建 x 值\n",
    "x_values = np.linspace(-5, 5, 400)\n",
    "\n",
    "# 绘图\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(x_values, f(x_values), label='$f\\;(x) = x^2 + 3x + 2$', color='blue')\n",
    "plt.plot(x_values, f_prime(x_values), label=\"$f\\;'(x) = 2x + 3$\", color='red', linestyle='--')\n",
    "\n",
    "# 添加标签和标题\n",
    "plt.title('函数及其导数')\n",
    "plt.xlabel('$x$')\n",
    "plt.ylabel('$y$')\n",
    "plt.axhline(0, color='black',linewidth=0.5, ls='--')\n",
    "plt.axvline(0, color='black',linewidth=0.5, ls='--')\n",
    "plt.grid()\n",
    "plt.legend()\n",
    "plt.show()\n"
   ],
   "id": "72528da6b7be3aca",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "plt.figure(figsize=(8, 6), facecolor='white')\n",
    "\n",
    "# 定义函数\n",
    "def f(x):\n",
    "    return x**2 + 3*x + 2\n",
    "\n",
    "# 计算函数的导数\n",
    "def f_prime(x):\n",
    "    return 2*x + 3\n",
    "\n",
    "# 绘制函数曲线\n",
    "x_values = np.linspace(-5, 5, 400)\n",
    "y_values = f(x_values)\n",
    "\n",
    "plt.plot(x_values, y_values, label='$f\\;(x) = x^2 + 3x + 2$')\n",
    "\n",
    "# 选择一点 P(x0, f(x0))\n",
    "x0 = 1\n",
    "y0 = f(x0)\n",
    "slope = f_prime(x0)\n",
    "\n",
    "# 绘制切线\n",
    "x_tangent = np.linspace(x0 - 1, x0 + 1, 100)\n",
    "y_tangent = slope * (x_tangent - x0) + y0\n",
    "plt.plot(x_tangent, y_tangent, 'r--', label='切线')\n",
    "\n",
    "# 标记点 P\n",
    "plt.scatter(x0, y0, color='blue', label='$P$')\n",
    "\n",
    "# 添加图例和网格\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.xlabel('$x$')\n",
    "plt.ylabel('$y$')\n",
    "# plt.title('导数的几何意义')\n",
    "plt.show()\n"
   ],
   "id": "5ab7dbb3b3750d0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "ef37c0e8ab570144",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "b098fd189c703e7d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "812821a60ecc463b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "61145eb0a4172217",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 初始化设置\n",
    "plt.figure(figsize=(8, 6), facecolor='white')\n",
    "plt.rcParams['font.sans-serif'] = ['KaiTi']  # 中文字体\n",
    "plt.rcParams['mathtext.fontset'] = 'stix'     # 数学字体\n",
    "\n",
    "# 生成运动轨迹（抛物线）\n",
    "t = np.linspace(0, 4, 100)\n",
    "s = 2 * t**2  # 位移函数\n",
    "\n",
    "# 关键点参数\n",
    "t1, t2 = 1.0, 3.0  # 割线端点\n",
    "tn = 2.0           # 切点位置\n",
    "\n",
    "# 速度计算\n",
    "k1 = (s[int(t2 * 25)] - s[int(t1 * 25)]) / (t2 - t1)  # 平均速度\n",
    "k12 = 4 * tn  # 瞬时速度（导数值）\n",
    "\n",
    "# 创建画布\n",
    "ax = plt.gca()\n",
    "ax.spines['right'].set_color('none')\n",
    "ax.spines['top'].set_color('none')\n",
    "ax.xaxis.set_ticks_position('bottom')\n",
    "ax.yaxis.set_ticks_position('left')\n",
    "\n",
    "# 绘制运动轨迹\n",
    "plt.plot(t, s, 'b-', lw=2, label='运动轨迹')\n",
    "\n",
    "# 绘制割线\n",
    "plt.plot([t1, t2], [s[int(t1 * 25)], s[int(t2 * 25)]], \n",
    "         'g-', lw=2, label='割线')\n",
    "\n",
    "# 绘制切线\n",
    "tangent = k12 * (t - tn) + 2 * tn**2  # 切线公式更新\n",
    "plt.plot(t, tangent, 'r--', lw=2, alpha=0.7, label='切线')\n",
    "\n",
    "# 标注关键点\n",
    "plt.scatter(tn, 2 * tn**2, color='black', s=50)\n",
    "plt.text(tn + 0.1, 2 * tn**2, 'n', color='black', fontsize=12)  # 调整标注的位置\n",
    "\n",
    "# 坐标系统\n",
    "plt.xlim([0, 4])\n",
    "plt.ylim([0, 32])\n",
    "plt.xticks(np.arange(0, 5, 1))\n",
    "plt.yticks(np.arange(0, 33, 4))\n",
    "plt.xlabel('时间 $(t)$', fontsize=12)\n",
    "plt.ylabel('距离 $(s)$', fontsize=12)\n",
    "\n",
    "# 图例与布局\n",
    "plt.legend(loc='upper left')\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show() \n"
   ],
   "id": "c598150124d7e3a4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "b88617eb8c4c46f2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "3cded5f58f7dc069",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams.update({\n",
    "    'font.size': 14,          # 全局字体大小\n",
    "    'axes.titlesize': 16,     # 标题字体大小\n",
    "    'axes.labelsize': 14,     # 坐标轴标签字体大小\n",
    "    'xtick.labelsize': 12,    # x轴刻度字体\n",
    "    'ytick.labelsize': 12,    # y轴刻度字体\n",
    "    'legend.fontsize': 14     # 图例字体\n",
    "})\n",
    "plt.rcParams['font.sans-serif'] = ['KaiTi'] \n",
    "plt.rcParams['mathtext.fontset'] = 'stix'  # 数学字体\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题\n",
    "\n",
    "\n",
    "# 定义分段函数\n",
    "def f(x):\n",
    "    return np.where(x < 0, -x, x**2)\n",
    "\n",
    "# 定义导数\n",
    "left_derivative = -1  # 左导数 (x < 0时的导数值)\n",
    "right_derivative = 0   # 右导数 (x >= 0时的导数值)\n",
    "\n",
    "# 生成数据点\n",
    "x_left = np.linspace(-2, 0, 100)\n",
    "x_right = np.linspace(0, 2, 100)\n",
    "y_left = f(x_left)\n",
    "y_right = f(x_right)\n",
    "\n",
    "# 创建画布\n",
    "plt.figure(figsize=(10, 6))\n",
    "\n",
    "# 绘制原函数\n",
    "plt.plot(x_left, y_left, label='$f\\;(x) = -x (x < 0)$', color='blue')\n",
    "plt.plot(x_right, y_right, label='$f\\;(x) = x^2 (x ≥ 0)$', color='orange')\n",
    "plt.scatter(0, f(0), color='red', zorder=5)  # 关键点\n",
    "\n",
    "# 绘制左右导数切线\n",
    "tangent_left = left_derivative * (x_left - 0) + f(0)  # 左切线方程\n",
    "tangent_right = right_derivative * (x_right - 0) + f(0)  # 右切线方程\n",
    "\n",
    "plt.plot(x_left, tangent_left, '--', color='darkblue', label='左导数切线 (斜率=-1)')\n",
    "plt.plot(x_right, tangent_right, ':', color='darkorange', label='右导数切线 (斜率=0)')\n",
    "\n",
    "# 标注说明\n",
    "plt.annotate('左导数存在但不相等于\\n右导数，因此函数在 $x=0$ 处\\n不可导', \n",
    "             xy=(0, 0), xytext=(0.5, 1.5),\n",
    "             arrowprops=dict(facecolor='black', shrink=0.05))\n",
    "\n",
    "# 设置图形属性\n",
    "plt.title('左导数与右导数')\n",
    "plt.xlabel('$x$')\n",
    "plt.ylabel('$f\\;(x)$')\n",
    "plt.grid(True, linestyle='--', alpha=0.7)\n",
    "plt.legend()\n",
    "plt.axis([-2, 2, -1, 5])\n",
    "plt.show()\n"
   ],
   "id": "ea6c71cda83cc0b0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "e23699e63ceb0bac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.linspace(-5, 5, 1000)\n",
    "y = np.maximum(0, x)\n",
    "\n",
    "plt.plot(x, y, linewidth=2, color='black')\n",
    "plt.scatter(0, 0, color='red', zorder=3) # 突出拐点\n",
    "plt.title(\"$ReLU \\;\\;Function: f\\;(x) = \\max(0, x)$\")\n",
    "plt.xlabel(\"$x$\"), plt.ylabel(\"$f\\;(x)$\")\n",
    "plt.grid(True)\n",
    "plt.show()"
   ],
   "id": "5960c1dc87c99411",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "f76a30d939689031",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "894873985def4a12",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "ff1038429304d850",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数\n",
    "def f(x):\n",
    "    return x**2\n",
    "\n",
    "def f_prime(x):\n",
    "    return 2 * x\n",
    "\n",
    "# 生成 x 的区间\n",
    "x = np.linspace(-1.5, 1.5, 400)\n",
    "\n",
    "# 计算函数值和导数值\n",
    "y = f(x)\n",
    "y_prime = f_prime(x)\n",
    "\n",
    "# 绘制函数 f(x)\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.plot(x, y, label='$f\\;(x) = x^2$', color='blue')\n",
    "plt.axhline(0, color='black',linewidth=0.5)\n",
    "plt.axvline(0, color='black',linewidth=0.5)\n",
    "plt.grid(color = 'gray', linestyle = '--', linewidth = 0.5)\n",
    "\n",
    "# 绘制导数 f'(x)\n",
    "# plt.figure(figsize=(8, 6))\n",
    "plt.plot(x, y_prime, label=\"$f\\;'(x) = 2x$\", color='red')\n",
    "plt.axhline(0, color='black', linewidth=0.5)\n",
    "plt.axvline(0, color='black', linewidth=0.5)\n",
    "plt.grid(color='gray', linestyle='--', linewidth=0.5)\n",
    "plt.title(\"导数与函数的单调性\")\n",
    "plt.xlabel('x')\n",
    "plt.ylabel(\"y\")\n",
    "plt.legend()\n",
    "plt.show()\n"
   ],
   "id": "822b7894bd88e7ff",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# plt.rcParams.update({\n",
    "#     'font.size': 14,          # 全局字体大小\n",
    "#     'axes.titlesize': 16,     # 标题字体大小\n",
    "#     'axes.labelsize': 14,     # 坐标轴标签字体大小\n",
    "#     'xtick.labelsize': 12,    # x轴刻度字体\n",
    "#     'ytick.labelsize': 12,    # y轴刻度字体\n",
    "#     'legend.fontsize': 20     # 图例字体\n",
    "# })\n",
    "# 初始化设置\n",
    "plt.rcParams['font.sans-serif'] = ['KaiTi']  # 中文字体\n",
    "plt.rcParams['mathtext.fontset'] = 'stix'     # 数学字体\n",
    "\n",
    "\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题\n"
   ],
   "id": "56ceaf260a529ed1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数\n",
    "def f(x):\n",
    "    return x**3\n",
    "\n",
    "def f_prime(x):\n",
    "    return 3 * x**2\n",
    "\n",
    "# 生成 x 的区间\n",
    "x = np.linspace(-2, 2, 400)\n",
    "\n",
    "# 计算函数值和导数值\n",
    "y = f(x)\n",
    "y_prime = f_prime(x)\n",
    "\n",
    "# 绘制函数 f(x)\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.plot(x, y, label='$f\\;(x) = x^3$', color='blue')\n",
    "plt.axhline(0, color='black', linewidth=0.5)\n",
    "plt.axvline(0, color='black', linewidth=0.5)\n",
    "plt.grid(color='gray', linestyle='--', linewidth=0.5)\n",
    "# plt.title('Function $f(x) = x^3$ and its derivative')\n",
    "# plt.xlabel('x')\n",
    "# plt.ylabel('f(x)')\n",
    "# plt.legend()\n",
    "# plt.show()\n",
    "\n",
    "# 绘制导数 f'(x)\n",
    "# plt.figure(figsize=(8, 6))\n",
    "plt.plot(x, y_prime, label=\"$f\\;'(x) = x^3$\", color='red')\n",
    "\n",
    "plt.plot(0, 0, 'ro', label=r'拐点 $(0, 0)$')\n",
    "\n",
    "\n",
    "plt.axhline(0, color='black', linewidth=0.5)\n",
    "plt.axvline(0, color='black', linewidth=0.5)\n",
    "plt.grid(color='gray', linestyle='--', linewidth=0.5)\n",
    "plt.title(\"$f\\;(x) = x^3$的原函数与导函数\")\n",
    "plt.xlabel('x')\n",
    "plt.ylabel(\"y\")\n",
    "plt.legend()\n",
    "plt.show()\n"
   ],
   "id": "cbf3b98dc69fdb8d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数 f(x) = x^3 - 3x^2 + 2x\n",
    "def f(x):\n",
    "    return x**3 - 3*x**2 + 2*x\n",
    "\n",
    "# 定义导数 f'(x) = 3x^2 - 6x + 2\n",
    "def f_prime(x):\n",
    "    return 3*x**2 - 6*x + 2\n",
    "\n",
    "# 定义二阶导数 f''(x) = 6x - 6\n",
    "def f_double_prime(x):\n",
    "    return 6*x - 6\n",
    "\n",
    "# 求导数为零的点 （即解方程 f'(x) = 0）\n",
    "from sympy import symbols, solve\n",
    "\n",
    "x = symbols('x')\n",
    "critical_points = solve(f_prime(x), x)\n",
    "critical_points = [float(point) for point in critical_points]\n",
    "\n",
    "# 计算极值点的值\n",
    "extrema_values = [f(point) for point in critical_points]\n",
    "\n",
    "# 分析二阶导数判断极值类型\n",
    "extrema_types = []\n",
    "for point in critical_points:\n",
    "    if f_double_prime(point) > 0:\n",
    "        extrema_types.append('Local Minimum')\n",
    "    elif f_double_prime(point) < 0:\n",
    "        extrema_types.append('Local Maximum')\n",
    "    else:\n",
    "        extrema_types.append('Neither Min nor Max')\n",
    "\n",
    "# 绘制函数 f(x) 和其导数 f'(x)\n",
    "x_values = np.linspace(-1, 4, 400)\n",
    "f_values = f(x_values)\n",
    "f_prime_values = f_prime(x_values)\n",
    "\n",
    "plt.figure(figsize=(12, 6))\n",
    "\n",
    "# 绘制 f(x)\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.plot(x_values, f_values, label=r'$f(x) = x^3 - 3x^2 + 2x$', color='blue')\n",
    "plt.scatter(critical_points, extrema_values, color='red', label='Critical Points')\n",
    "for i, point in enumerate(critical_points):\n",
    "    plt.annotate(f'{extrema_types[i]}: ({point:.2f}, {extrema_values[i]:.2f})',\n",
    "                 (point, extrema_values[i]), textcoords=\"offset points\", xytext=(0,10))\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('f(x)')\n",
    "plt.title('Function $f(x)$ with Critical Points')\n",
    "plt.legend()\n",
    "\n",
    "# 绘制 f'(x)\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.plot(x_values, f_prime_values, label=r\"$f'(x) = 3x^2 - 6x + 2$\", color='green')\n",
    "plt.axhline(0, color='gray', linestyle='--')  # 添加水平参考线 y=0\n",
    "plt.scatter(critical_points, [0]*len(critical_points), color='red', label='Critical Points')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel(\"f'(x)\")\n",
    "plt.title(\"Derivative $f'(x)$\")\n",
    "plt.legend()\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n",
    "# 输出极值点信息\n",
    "for i, point in enumerate(critical_points):\n",
    "    print(f\"Critical Point: x = {point:.2f} -> f(x) = {extrema_values[i]:.2f} ({extrema_types[i]})\")\n"
   ],
   "id": "5cd050d917a5b714",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数 y = x^2\n",
    "def f(x):\n",
    "    return x ** 2\n",
    "\n",
    "# 定义导数 f'(x) = 2x\n",
    "def df(x):\n",
    "    return 2 * x\n",
    "\n",
    "# 生成 x 的取值范围\n",
    "x = np.linspace(-3, 3, 1000)\n",
    "\n",
    "# 计算 y 和导数\n",
    "y = f(x)\n",
    "dy = df(x)\n",
    "\n",
    "# 绘制函数图像\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(x, y, label=r'$f\\;(x) = x^2$', color='blue')\n",
    "\n",
    "# 标记极值点\n",
    "x_critical = 0  # f'(x) = 0 时，x = 0\n",
    "y_critical = f(x_critical)\n",
    "\n",
    "# 绘制导数图像\n",
    "# plt.figure(figsize=(10, 6))\n",
    "plt.plot(x, dy, label=r\"$f\\;'(x) = 2x$\", color='green')\n",
    "# plt.plot(x_critical, df(x_critical), 'ro', label=r'导数为零点 $(0, 0)$')\n",
    "\n",
    "# 添加标注和标题\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('y')\n",
    "plt.title(\"$f\\;(x) = x^2$的原函数与导函数\")\n",
    "\n",
    "plt.plot(x_critical, y_critical, 'ro', label=r'极值点 $(0, 0)$')\n",
    "\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.axhline(0, color='gray',linewidth=0.8)\n",
    "plt.axvline(0, color='gray',linewidth=0.8)\n",
    "plt.show()\n"
   ],
   "id": "ed21cdd896f247e5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "5d0da8b0029e8a92",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "4b771ac43a7c3a18",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "14589b26e01881be",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import plotly.graph_objects as go\n",
    "import numpy as np\n",
    "\n",
    "# 定义函数\n",
    "def f(x, y, z):\n",
    "    return 2*x**2 - x*y + y**2 - 3*z**2\n",
    "\n",
    "# 生成三维网格\n",
    "x = np.linspace(-5, 5, 50)\n",
    "y = np.linspace(-5, 5, 50)\n",
    "z = np.linspace(-5, 5, 50)\n",
    "X, Y, Z = np.meshgrid(x, y, z)\n",
    "F = f(X, Y, Z)\n",
    "\n",
    "# 绘制等值面（F=0）\n",
    "fig = go.Figure(data=go.Isosurface(\n",
    "    x=X.flatten(), y=Y.flatten(), z=Z.flatten(),\n",
    "    value=F.flatten(),\n",
    "    isomin=0,\n",
    "    isomax=0,\n",
    "    surface_count=1,\n",
    "    colorscale='Blues',\n",
    "    opacity=0.6\n",
    "))\n",
    "\n",
    "fig.update_layout(\n",
    "    title='f(x,y,z)=0 的等值面',\n",
    "    scene=dict(xaxis_title='X轴', yaxis_title='Y轴', zaxis_title='Z轴')\n",
    ")\n",
    "fig.show()\n"
   ],
   "id": "5e09a9b201584861",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def f(x, y, z):\n",
    "    return 2*x**2 - x*y + y**2 - 3*z**2\n",
    "\n",
    "# 定义不同z值的截面\n",
    "z_values = [-2, 0, 2]\n",
    "x = np.linspace(-5, 5, 100)\n",
    "y = np.linspace(-5, 5, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "\n",
    "# 绘制多个子图\n",
    "fig, axes = plt.subplots(1, 3, figsize=(18, 6))\n",
    "for ax, z in zip(axes, z_values):\n",
    "    F = f(X, Y, z)\n",
    "    contour = ax.contourf(X, Y, F, levels=20, cmap='viridis')\n",
    "    ax.contour(X, Y, F, levels=[0], colors='red')  # 标出f=0的等高线\n",
    "    ax.set_xlabel('X轴')\n",
    "    ax.set_ylabel('Y轴')\n",
    "    ax.set_title(f'z = {z}')\n",
    "    plt.colorbar(contour, ax=ax)\n",
    "\n",
    "plt.suptitle('不同z值的函数截面图（红色为f=0）')\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ],
   "id": "58b406d1ece33373",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.linspace(-5, 5, 100)\n",
    "y = np.linspace(-5, 5, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "F = 2*X**2 - X*Y + Y**2\n",
    "\n",
    "# 绘制3D曲面\n",
    "fig = plt.figure(figsize=(10, 7))\n",
    "ax = fig.add_subplot(111, projection='3d')\n",
    "ax.plot_surface(X, Y, F, cmap='viridis', alpha=0.8)\n",
    "ax.set_xlabel('X轴')\n",
    "ax.set_ylabel('Y轴')\n",
    "ax.set_zlabel('f(x,y)')\n",
    "ax.set_title('z=0时的函数曲面')\n",
    "plt.show()\n"
   ],
   "id": "df4f2bf498b203b7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.misc import derivative\n",
    "\n",
    "# 定义函数及其导数\n",
    "def f(x):\n",
    "    return x**2  # 示例函数（可替换为其他函数）\n",
    "\n",
    "def df(x):\n",
    "    return derivative(f, x, dx=1e-6)  # 数值计算一阶导数\n",
    "\n",
    "def d2f(x):\n",
    "    return derivative(f, x, dx=1e-6, n=2)  # 数值计算二阶导数\n",
    "\n",
    "# 生成数据\n",
    "x = np.linspace(-2, 2, 100)\n",
    "y = f(x)\n",
    "dy = [df(xi) for xi in x]\n",
    "d2y = [d2f(xi) for xi in x]\n",
    "\n",
    "# 绘制图形\n",
    "plt.figure(figsize=(12, 8))\n",
    "\n",
    "# 函数曲线\n",
    "plt.plot(x, y, label=\"$f\\;(x) = x^2$\", linewidth=3, color='blue')\n",
    "\n",
    "# 一阶导数曲线\n",
    "plt.plot(x, dy, label=\"$f\\;'(x) = 2x$\", linestyle='--', color='green')\n",
    "\n",
    "# 二阶导数曲线（常数）\n",
    "plt.plot(x, d2y, label=\"$f\\;''(x) = 2$\", linestyle=':', color='red')\n",
    "\n",
    "# 标注关键点\n",
    "x0 = 0\n",
    "plt.scatter(x0, f(x0), c='black', s=100, zorder=5, \n",
    "           label=f\"极值点 ($f\\;'(0)=0$, $f\\;''(0)=2>0$)\")\n",
    "\n",
    "# 坐标轴和标签\n",
    "plt.axhline(0, color='black', linewidth=0.5)\n",
    "plt.axvline(0, color='black', linewidth=0.5)\n",
    "plt.title(\"一元函数: $f\\;(x) = -x^2$ 极值判别\", fontsize=14)\n",
    "plt.xlabel(\"x\", fontsize=18)\n",
    "plt.ylabel(\"y\", fontsize=18)\n",
    "plt.legend(loc='upper left', fontsize=18)\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.show()"
   ],
   "id": "ce1a77be90c703b1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.misc import derivative\n",
    "\n",
    "# 定义函数及其导数\n",
    "def f(x):\n",
    "    return -x**2  # 示例函数（可替换为其他函数）\n",
    "\n",
    "def df(x):\n",
    "    return derivative(f, x, dx=1e-6)  # 数值计算一阶导数\n",
    "\n",
    "def d2f(x):\n",
    "    return derivative(f, x, dx=1e-6, n=2)  # 数值计算二阶导数\n",
    "\n",
    "# 生成数据\n",
    "x = np.linspace(-2, 2, 100)\n",
    "y = f(x)\n",
    "dy = [df(xi) for xi in x]\n",
    "d2y = [d2f(xi) for xi in x]\n",
    "\n",
    "# 绘制图形\n",
    "plt.figure(figsize=(12, 8))\n",
    "\n",
    "# 函数曲线\n",
    "plt.plot(x, y, label=\"$f\\;(x) = -x^2$\", linewidth=3, color='blue')\n",
    "\n",
    "# 一阶导数曲线\n",
    "plt.plot(x, dy, label=\"$f\\;'(x) = -2x$\", linestyle='--', color='green')\n",
    "\n",
    "# 二阶导数曲线（常数）\n",
    "plt.plot(x, d2y, label=\"$f\\;''(x) = -2$\", linestyle=':', color='red')\n",
    "\n",
    "# 标注关键点\n",
    "x0 = 0\n",
    "plt.scatter(x0, f(x0), c='black', s=100, zorder=5, \n",
    "           label=f\"极值点 ($f\\;'(0)=0$, $f\\;''(0)=-2<0$)\")\n",
    "\n",
    "# 坐标轴和标签\n",
    "plt.axhline(0, color='black', linewidth=0.5)\n",
    "plt.axvline(0, color='black', linewidth=0.5)\n",
    "plt.title(\"一元函数: $f\\;(x) = -x^2$ 的极值判别\", fontsize=14)\n",
    "plt.xlabel(\"x\", fontsize=18)\n",
    "plt.ylabel(\"y\", fontsize=18)\n",
    "plt.legend(loc='upper left', fontsize=18)\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.show()"
   ],
   "id": "c4c3d54f90e8c0c8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数\n",
    "def f(x, y):\n",
    "    return 2*x**2 + 3*y**2 - 4*x - 6*y + 5\n",
    "\n",
    "# 生成网格数据\n",
    "x = np.linspace(0, 2, 100)\n",
    "y = np.linspace(0, 2, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "Z = f(X, Y)\n",
    "\n",
    "# 绘制等高线图\n",
    "plt.figure(figsize=(8, 6))\n",
    "contour = plt.contour(X, Y, Z, levels=20, cmap='viridis')\n",
    "plt.clabel(contour, inline=True, fontsize=8)\n",
    "plt.scatter(1, 1, c='red', s=50, label=\"极小值点 (1,1)\")\n",
    "plt.xlabel(\"x\", fontsize=12)\n",
    "plt.ylabel(\"y\", fontsize=12)\n",
    "plt.title(\"函数 $f(x,y) = 2x^2 + 3y^2 - 4x - 6y + 5$ 的等高线图\", fontsize=10)\n",
    "plt.legend()\n",
    "plt.grid(alpha=0.3)\n",
    "plt.show()"
   ],
   "id": "f916cb49446d0c82",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 定义函数\n",
    "def f(x, y):\n",
    "    return 2*x**2 + 3*y**2 - 4*x - 6*y + 5\n",
    "\n",
    "# 计算极值点（通过求导）\n",
    "def find_minimum():\n",
    "    x_min = 4/(2*2)  # 解方程 4x - 4 = 0\n",
    "    y_min = 6/(2*3)  # 解方程 6y - 6 = 0\n",
    "    return x_min, y_min, f(x_min, y_min)\n",
    "\n",
    "# 生成网格数据\n",
    "x = np.linspace(-2, 4, 100)\n",
    "y = np.linspace(-2, 4, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "Z = f(X, Y)\n",
    "\n",
    "# 计算极值点坐标\n",
    "x_min, y_min, z_min = find_minimum()\n",
    "\n",
    "# 创建3D画布\n",
    "fig = plt.figure(figsize=(12, 8))\n",
    "ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "# 绘制曲面\n",
    "surf = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8, \n",
    "                      rstride=5, cstride=5)  # 降低网格密度\n",
    "\n",
    "# 标注极值点\n",
    "ax.scatter(x_min, y_min, z_min, c='red', s=100, \n",
    "          label=f'极小值点\\n({x_min:.1f}, {y_min:.1f}, {z_min:.1f})')\n",
    "\n",
    "# 美化图形\n",
    "ax.set_xlabel('X轴', fontsize=12, labelpad=15)\n",
    "ax.set_ylabel('Y轴', fontsize=12, labelpad=15)\n",
    "ax.set_zlabel('Z轴', fontsize=12, labelpad=15)\n",
    "ax.set_title('$f\\;(x,y) = 2x^2 + 3y^2 - 4x - 6y + 5$ 的3D图形', fontsize=14, pad=20)\n",
    "ax.legend(loc='upper left', fontsize=10)\n",
    "\n",
    "# 添加颜色条\n",
    "fig.colorbar(surf, shrink=0.5, aspect=5, pad=0.1)\n",
    "\n",
    "# 调整观察角度\n",
    "ax.view_init(elev=30, azim=-45)  # elev仰角, azim方位角\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ],
   "id": "973864bc586fc70e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "A = np.array([[3, 1], [1, 3]])\n",
    "v1 = np.array([1, -1])  # 特征向量1\n",
    "v2 = np.array([1, 1])   # 特征向量2\n",
    "\n",
    "plt.figure(figsize=(8, 4))\n",
    "plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='v1 (λ=2)')\n",
    "plt.quiver(0, 0, v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color='b', label='v2 (λ=4)')\n",
    "plt.xlim(-2, 2)\n",
    "plt.ylim(-2, 2)\n",
    "plt.grid()\n",
    "plt.title(\"特征向量方向演示\")\n",
    "plt.legend()\n",
    "plt.show()"
   ],
   "id": "be68b3aa013e3c25",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "844ab7f8e1a5445f",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
